//@author A0088498A
package logic;

import common.Command;

import memory.History;

/**
 * The Command Recaller allows recall previous commands typed by users stored in
 * the History <br/>
 * 
 */
public class CommandRecaller {
	private static CommandRecaller INSTANCE = null;
	private History _history;

	/**
	 * Private constructor to prevent others to instantiate more than 1
	 * instances<br/>
	 */
	private CommandRecaller() {
		_history = History.getInstance();
	}

	/**
	 * Getter of the singleton
	 * 
	 * @return the singleton o this class
	 */
	public static CommandRecaller getInstance() {
		// Lazy instantiation
		if (INSTANCE == null) {
			INSTANCE = new CommandRecaller();
		}

		return INSTANCE;
	}

	/**
	 * Get the previous command typed by user before the pointer <br/>
	 * 
	 * @return Raw command string typed by user. If no command is found (i.e:
	 *         the last command popped is the last command in the stack), null
	 *         is returned
	 */
	public String recallPreviousCommand() {
		Command command;
		command = _history.movePointerBack();
		if (command != null) {
			return command.getCommandString();
		} else {
			return null;
		}
	}

	/**
	 * Get the next command typed by user after the pointer <br/>
	 * 
	 * @return Raw command string typed by user. If no command is found (i.e:
	 *         the last command popped is the latest command), null is returned
	 */
	public String recallNextCommand() {
		Command command;
		command = _history.movePointerFront();
		if (command != null) {
			return command.getCommandString();
		} else {
			return null;
		}
	}

	/**
	 * Restore the position of pointer back to the top of command stack <br/>
	 */
	public void resetPointer() {
		_history.restorePointer();
	}
}
